package com.aperos.webservice;

import java.io.IOException;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings("serial")
public class GetAllBarsServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(GetAllBarsServlet.class.getName());

    @SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	
    	String xml = req.getParameter("xml");
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
        String query = "select from " + Bar.class.getName();
        List<Bar> bars = (List<Bar>) pm.newQuery(query).execute();
        if (bars.isEmpty()) {
        	if (xml != null) {
        		resp.setContentType("text/xml; charset=UTF-8");
        	} else {
        		resp.setContentType("text/plain; charset=UTF-8");
            	resp.getWriter().println("No bar");
        	}
        } else {
        	
        	if (xml != null) {
        		resp.setContentType("text/xml; charset=UTF-8");
        		resp.getWriter().println("<bars>");
        	} else {
        		resp.setContentType("text/plain; charset=UTF-8");
        	}
        	
            for (Bar bar : bars) {
            	if (xml != null) {
            		resp.getWriter().println("<bar>");
            		resp.getWriter().println("<id>" + String.valueOf(bar.getKey().getId()) + "</id>");
            		
            		String name = bar.getName().replace("&", "&amp;");
            		resp.getWriter().println("<name>" + name + "</name>");
            		
            		if (bar.getAddress() != null) {
            			String address = bar.getAddress().replace("&", "&amp;");
            			resp.getWriter().println("<address>" + address + "</address>");
                	}
                	if (bar.getPriceBeer() != null) {
                		resp.getWriter().println("<priceBeer>" + bar.getPriceBeer().toString() + "</priceBeer>");
                	}
                	if (bar.getPriceCocktail() != null) {
                		resp.getWriter().println("<priceCocktail>" + bar.getPriceCocktail().toString() + "</priceCocktail>");
                	}
                	if (bar.getHappyHourBeginHour() != null) {
                		resp.getWriter().println("<happyHourBeginHour>" + bar.getHappyHourBeginHour().toString() + "</happyHourBeginHour>");
                	}
                	if (bar.getHappyHourBeginMinute() != null) {
                		resp.getWriter().println("<happyHourBeginMinute>" + bar.getHappyHourBeginMinute().toString() + "</happyHourBeginMinute>");
                	}
                	if (bar.getHappyHourEndHour() != null) {
                		resp.getWriter().println("<happyHourEndHour>" + bar.getHappyHourEndHour().toString() + "</happyHourEndHour>");
                	}
                	if (bar.getHappyHourEndMinute() != null) {
                		resp.getWriter().println("<happyHourEndMinute>" + bar.getHappyHourEndMinute().toString() + "</happyHourEndMinute>");
                	}
                	if (bar.getPriceBeerHappyHour() != null) {
                		resp.getWriter().println("<priceBeerHappyHour>" + bar.getPriceBeerHappyHour().toString() + "</priceBeerHappyHour>");
                	}
                	if (bar.getPriceCocktailHappyHour() != null) {
                		resp.getWriter().println("<priceCocktailHappyHour>" + bar.getPriceCocktailHappyHour().toString() + "</priceCocktailHappyHour>");
                	}
                	resp.getWriter().println("<latitudeE6>" + String.valueOf(bar.getLatitudeE6()).toString() + "</latitudeE6>");
            		resp.getWriter().println("<longitudeE6>" + String.valueOf(bar.getLongitudeE6()).toString() + "</longitudeE6>");
                	
                	if (bar.getMoney() != null) {
                		resp.getWriter().println("<money>" + bar.getMoney() + "</money>");
                	}
            		
                	resp.getWriter().println("</bar>");
            	} else {
            		String id = "1=" + String.valueOf(bar.getKey().getId());
                	String name = "2=" + bar.getName();
                	String address = "3=";
                	if (bar.getAddress() != null) {
                		address += bar.getAddress().toString();
                	}
                	String priceBeer = "4=";
                	if (bar.getPriceBeer() != null) {
                		priceBeer += bar.getPriceBeer().toString();
                	}
                	String priceCocktail = "5=";
                	if (bar.getPriceCocktail() != null) {
                		priceCocktail += bar.getPriceCocktail().toString();
                	}
                	String happyHourBeginHour = "6=";
                	if (bar.getHappyHourBeginHour() != null) {
                		happyHourBeginHour += bar.getHappyHourBeginHour().toString();
                	}
                	String happyHourBeginMinute = "7=";
                	if (bar.getHappyHourBeginMinute() != null) {
                		happyHourBeginMinute += bar.getHappyHourBeginMinute().toString();
                	}
                	String happyHourEndHour = "8=";
                	if (bar.getHappyHourEndHour() != null) {
                		happyHourEndHour += bar.getHappyHourEndHour().toString();
                	}
                	String happyHourEndMinute = "9=";
                	if (bar.getHappyHourEndMinute() != null) {
                		happyHourEndMinute += bar.getHappyHourEndMinute().toString();
                	}
                	String priceBeerHappyHour = "10=";
                	if (bar.getPriceBeerHappyHour() != null) {
                		priceBeerHappyHour += bar.getPriceBeerHappyHour().toString();
                	}
                	String priceCocktailHappyHour = "11=";
                	if (bar.getPriceCocktailHappyHour() != null) {
                		priceCocktailHappyHour += bar.getPriceCocktailHappyHour().toString();
                	}
                	String latitudeE6 = "12=" + String.valueOf(bar.getLatitudeE6()).toString();
                	String longitudeE6 = "13=" + String.valueOf(bar.getLongitudeE6()).toString();
                	
                	String money = "14=";
                	if (bar.getMoney() != null) {
                		money += bar.getMoney();
                	}
            		
            		
            		String response = id + "|" + name + "|" + address + "|" + priceBeer + "|" + priceCocktail + "|";
                	response += happyHourBeginHour + "|" + happyHourBeginMinute + "|" + happyHourEndHour + "|" + happyHourEndMinute + "|";
                	response += priceBeerHappyHour + "|" + priceCocktailHappyHour + "|" + latitudeE6 + "|" + longitudeE6 + "|" + money;
                	resp.getWriter().println(response);
            	}
            	
            	
        		
            }
            
            if (xml != null) {
            	resp.getWriter().println("</bars>");
            }
            
        }
        pm.close();

    }
}